<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/5/13
 * Time: 10:46
 */

namespace app\index\controller;

use app\index\Common;

class News extends Common {
    public function list(){
        $list = db('news')
            ->where('is_del',0)
            ->where('status',1)
            ->order('sort','asc')
            ->paginate(6);

        $page = $list->render();
        //转换成数据对象,否则会报错
        $list = $list->items();

        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['link'] = !empty($v['seo_link']) ? url('index/news/details',['id'=>$v['seo_link']]) : url('index/news/details',['id'=>$v['id']]);
                $list[$k]['day'] = date('d',$v['time']);
                $list[$k]['month'] = date('M',$v['time']);
                unset($list[$k]['seo_link']);
            }
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('empty','<div align="center">No data</div>');
        return $this->fetch();
    }

    public function details($id = ''){
        /*$info = db('news')
            ->where('id|seo_link',$id)
            ->where('is_del',0)
            ->where('status',1)
            ->find();

        if (!$info){
            //抛出异常
            abort(404, '');
        }

        $other = db('news')
            ->where('is_del',0)
            ->where('status',1)
            ->orderRaw('rand()')
            ->limit(3)
            ->field('id,title,cover,time')
            ->select();
        if (!empty($other)){
            foreach ($other as $k => $v){
                $other[$k]['time'] = date('M y,Y',$v['time']);
            }
        }
        $this->assign('other',$other);

        $info['day'] = date('d',$info['time']);
        $info['month'] = date('M',$info['time']);
        $this->assign('seo_title',$info['seo_title']);
        $this->assign('seo_keywords',$info['seo_keywords']);
        $this->assign('seo_describe',$info['seo_describe']);
        $this->assign('info',$info);*/
        return $this->fetch();
    }
}